package com.hotec.PROJECT_NAME.utils;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gemcap on 16/4/17.
 */
public class UtilsExpression {
    /**
     * 获取map结果集
     *
     * @param expression
     * @param variables
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> getMapResult(String expression, Map<String, Object> variables) {
        return getReuslt(expression, variables, Map.class);
    }

    /**
     * 获取boolean结果及
     *
     * @param expression
     * @param variables
     * @return
     */
    public static boolean getBooleanResult(String expression, Map<String, Object> variables) {
        Assert.isTrue(StringUtils.isNotBlank(expression), "表达式不能为空");
        return getReuslt(expression, variables, Boolean.class);
    }

    public static String getStringResult(String expression, Map<String, Object> variables) {
        Assert.isTrue(StringUtils.isNotBlank(expression), "表达式不能为空");
        return getReuslt(expression, variables, String.class);
    }

    private static <T> T getReuslt(String expression, Map<String, Object> variables, Class clazz) {
        Assert.notNull(variables);

        ExpressionParser parser = new SpelExpressionParser();

        EvaluationContext context = new StandardEvaluationContext();
        if (variables != null) {
            Iterator<Map.Entry<String, Object>> it = variables.entrySet().iterator();
            for (Iterator<String> iterator = variables.keySet().iterator(); iterator.hasNext(); ) {
                String key = iterator.next();
                context.setVariable(key, variables.get(key));
            }
        }
        Object x = parser.parseExpression(expression).getValue(context, clazz);
        return (T) x;
    }
static class F{
    public String random(List<String> list,int i){
      int x=  0+(int)(Math.random()*(list.size()));
        return list.get(x);
    }
    public String strs(String str1,String str2){
        return str1+str2;
    }
    public String dateAdd(Date date,int i){
        Date date1 = DateUtils.addDays(date, i);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format( date1);
    }
    public String subFix(String x,String i){
        return x+"_"+i+"_";
    }
}
    public static void main(String[] args) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("dayOfYear", 1);
        List<String> s= Arrays.asList("proc","b");
        map.put("A6_3_1", s);
        map.put("A6_3_3", "LLB");
        map.put("A6_3_6", "LLB");
        map.put("A6_3_2", new Date());
        map.put("F",new F());
        map.put("startDays", 1);
        for(int i=0;i<10;i++){
            String x=UtilsExpression.getStringResult("#F.random(#A6_3_1,#dayOfYear)",map);
            System.out.println(x);
        }
    }
//

//    public static void main(String[] args) {
//        String greetingExp = "Hello, #{ #user }";
//        //表达是解析器 -
//        ExpressionParser parser = new SpelExpressionParser();
//        //计算值
//        EvaluationContext context = new StandardEvaluationContext();
//        context.setVariable("user", "Gangyou");
//
//        Expression expression = parser.parseExpression(greetingExp,
//                new TemplateParserContext());
//        System.out.println(expression.getValue(context, String.class));
//    }
}
